package com.dxk.dao;

import com.dxk.pojo.Department;
import com.dxk.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;

import java.util.Map;
import java.util.Random;

@Repository
public class EmployeeDao {

    private  DepartmentDao departmentDao=null;
    @Autowired
    public EmployeeDao(DepartmentDao departmentDao) {
        this.departmentDao=departmentDao;
    }

    //通过Spring容器 获取DepartmentDao 对象，



     static private Map<Integer,Employee>  employee=null;
    static {


//        employee=new HashMap<Integer,Employee>();
//        employee.put(1001,new Employee(1001,"dxk1","a2537472303@qq.com",1,new Department(1001,"教学部")));
//        employee.put(1002,new Employee(1002,"dxk2","b2537472303@qq.com",0,new Department(1001,"教研部")));
//        employee.put(1003,new Employee(1003,"dxk3","c2537472303@qq.com",1,new Department(1002,"教育部")));
//        employee.put(1004,new Employee(1004,"dxk4","d2537472303@qq.com",0,new Department(1003,"信息部")));
//        employee.put(1005,new Employee(1005,"dxk5","e2537472303@qq.com",1,new Department(1004,"后期部")));


    }

    //员工id
    private Integer id=1006;

    //根据id 查找一个员工
    public Employee getEmployee(Integer id){
        if (id!=null){
            return employee.get(id);

        }
        return null;

    }
    //查找所有员工
    public Collection<Employee> getAllEmployee(){
        return employee.values();

    }

    //根据id 修改员工信息
    public void updateEmployeeById(Integer id,Employee employees){
        if (id!=null){
        employee.replace(id,employees);
        }

    }
    // 增加一个员工
    public void addEmployee(Employee employees){
        System.out.println(employees.getDepartment().getId());
        employees.setDepartment(departmentDao.getDepartmentById(employees.getDepartment().getId()));
        if (employees.getId()==null){employees.setId(id++);}
        employee.put(employees.getId(),employees);

    }
    //根据id删除一个员工
    public void deleteEmployee(Integer id){
        if(employee.containsKey(id)){
            employee.remove(id);
        }
    }


}
